var veg;
var shenfen;
var fur;
var meal;
var page_t1;
var page_t2;
var page_t3;
var lingshi = [];
$.get("veg_get", {
    page: '1'
}, function(res) {
    veg = JSON.parse(res);
});

$.get("fur_get", {
    page: '1'
}, function(res) {
    fur = JSON.parse(res);
});

$.get("meal_get", {
    page: '1'
}, function(res) {
    meal = JSON.parse(res);
});

$.get("page_total1", {
}, function(page_total1) {
    var page_json = JSON.parse(page_total1)
    var page_M = Math.ceil(page_json[0].pagea / 12)
    page_t1 = page_M
});

$.get("page_total2", {
}, function(page_total1) {
    var page_json = JSON.parse(page_total1)
    var page_M = Math.ceil(page_json[0].pagea / 12)
    page_t2 = page_M
});

$.get("page_total3", {
}, function(page_total1) {
    var page_json = JSON.parse(page_total1)
    var page_M = Math.ceil(page_json[0].pagea / 12)
    page_t3 = page_M
});
window.onload=function (){
    var username;
    var designW = 960;  //设计稿宽
    var font_rate = 14;
    //适配
    document.getElementsByTagName("html")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
    document.getElementsByTagName("body")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";

    //监测窗口大小变化
    window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function() {
        document.getElementsByTagName("html")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
        document.getElementsByTagName("body")[0].style.fontSize = document.body.offsetWidth / designW * font_rate + "px";
    }, false);

    //获取用户名
    $.get("login_check", {
    }, function(res) {
        if(res != 2){
            $("#dengl").text("个人中心");
            $("#dengl1").text("个人中心");
            $("#dengl").attr('href','/html/myorder.html');
            $("#qita").text("退出");
            $("#qita1").text("退出");
            $("#qita").attr('href','/html/1.html');
            username = res;
            $.get("login_shenfen", {
                username:username
            }, function(res) {
                shenfen = res;
                if (res == 1) {
                    $("#denglua").css("display", "block")
                    $("#denglub").css("display", "none")
                    $("#jinggao").css("display", "none")
                }
                else {
                    $("#denglub").css("display", "block")
                    $("#denglua").css("display", "none")
                    $("#jinggao").css("display", "none")
                    $("#mysale").text("我的商品")
                    $(".button").text("")
                }
            });
        }
        else {
            $("#dengl").text("登录");
            $("#dengl1").text("登录");
            $("#dengl").attr('href','/html/1.html');
            $("#qita").text("注册");
            $("#qita1").text("注册");
            $("#qita").attr('href','/html/zhuce.html');
            $("#denglua").css("display","none")
            $("#denglub").css("display","none")
            $("#jinggao").css("display","block")
        }
    });

    //清除session
    $("#qita").click(function () {
        $.post("session-d", {
        }, function(res) {

        });
    })

    //监听窗口滚动
    $(window).scroll(function() {
        var scrollTop = $(this).scrollTop();

        if(scrollTop >= 100  ){
            $(".center_right").css({"position":"fixed","right":"0","top":"0"});
            $(".slef_login").css({"height":"6rem"});
            $(".retrun_top").stop(!0,!1).animate({right:'100px'},200);
        }

        if(scrollTop < 100 ){
            $(".center_right").css("position","static");
            $(".slef_login").css({"height":"12rem"});
            $(".retrun_top").stop(!0,!1).animate({right:'0px'},200);
        }

        if ( scrollTop == 0 ){
            $("#huidao").html("回   到")
            $("#dingbu").html("顶   部")
        }
    });

    //回到顶部的hover事件
    $(".return_top_a").hover(function () {
        $(".return_show").css("display","none");
        $(".return_hid").css("display","block")
    },function(){
        $(".return_show").css("display","block");
        $(".return_hid").css("display","none")
    })

    //回到顶部的click事件
    $(".return_top_a").click(function () {
        $('html , body').animate({scrollTop: 0},'slow');
        $("#huidao").html("冲   鸭")
        $("#dingbu").html("↑↑↑")
    })


    $(".jingyan").hover(function () {
        $(".jingyan_mini").css("opacity","1");
    },function(){
        $(".jingyan_mini").css("opacity","0");
    })

    //主页面图片切换
    var dingshi = setInterval(qhuan,7000);
    var yeshu = 1;
    var length = 3;
    var zancun = 0;

    $(".biaoqian").children("a").click(function () {
        bian();
        var dianji = $(this).attr("id").substring(6,7);
        yeshu = dianji;
        move();
        clearInterval(dingshi);
        dingshi = null;
        dingshi = setInterval(qhuan,7000);
    })

    function move() {
        $("#tupian"+yeshu).text("●");
        setTimeout(function(){
            $("#pian"+yeshu).css("display","block");
            $("#font"+yeshu).css("display","inline");
            $(".font_mover").css("transform"," translateX(0%)")
        },1400);
        setTimeout(function(){
            $(".yuan_w").css("z-index","1")
        },2700);
    };

    function bian() {
        $("#tupian"+yeshu).text("○");
        $("#pian"+yeshu).css("transform","scale(1.1)");
        $(".yuan").css("stroke-width","11%");
        $(".yuan_w").css("z-index","20");
        $(".font_mover").css("transform","translateX(-100%)");
        zancun = yeshu;
        setTimeout(function(){
            $("#pian"+zancun).css("transform","scale(1)");
            $("#pian"+zancun).css("display","none");
            $("#font"+zancun).css("display","none");
            $(".yuan").css("stroke-width","0%");
        },1350);
    }

    function qhuan(){
        bian();
        if (yeshu == length ){
            yeshu = 1;
            move();
        }
        else{
            yeshu++;
            move();
        }
    }

    $.get("news_get",{
    },function (res) {
        var news = JSON.parse(res);
        for(var i = 0 ; i <= 24 ; i++ ){
            $("#li_a_"+(i+1)).attr('href','http://www.cainong.cc'+news[i].href);
            $("#li_a_"+(i+1)).text(news[i].text);
        }
    });

    $.get("goods_get", {
        page: '1'
    }, function(res) {
        var tidai = JSON.parse(res);
        var page_t;
        $.get("page_total", {
        }, function(page_total) {
            var page_json = JSON.parse(page_total)
            var page_M = Math.ceil(page_json[0].pagea/12)
            page_t = page_M
            var Main = {
                data() {
                    return {
                        tableData1: lingshi,
                        tableData2: lingshi,
                        tableData3: lingshi,
                        tableData4: lingshi,
                        tableDataa: lingshi,
                        tableDatab: lingshi,
                        tableDatac: lingshi,
                        tableDatad: lingshi,
                        multipleSelection: [],
                        money: '',
                        activeName: 'second',
                        activeName1: 'first1',
                        activeNames: ['1'],
                        pageSize:'1',
                        currentPage:'1',
                        activeIndex: '1',
                        activeIndex2: '1',
                        dialogVisible: false,
                        dialogVisible1: false,
                        res:tidai,
                        yeshu: page_t,
                        yeshu1: page_t1,
                        yeshu2: page_t2,
                        yeshu3: page_t3,
                        veg: veg,
                        fur: fur,
                        meal: meal,
                        img_src:  [ '/image/news1.jpg', '/image/news2.jpg', '/image/news3.jpg'],
                        prcie1: '',
                        title: '',
                        number2: '',
                        wareID: '',
                        form:{
                            biaoti: '',
                            price: '',
                            number: '',
                            username: username
                        },
                        fileList: [],
                        options: [{
                            value: 'A',
                            label: '蔬菜'
                        }, {
                            value: 'B',
                            label: '水果'
                        }, {
                            value: 'C',
                            label: '肉类'
                        }],
                        ware_c:{
                            title:'1',
                            price:'2',
                            number:'3'
                        }
                    };
                },
                methods: {
                    resetForm(formName) {
                        this.$refs[formName].resetFields();
                    },
                    submitUpload() {
                        var zhixiang = this;
                        if ( zhixiang.form.biaoti == '' || zhixiang.form.price == '' || zhixiang.form.number == '' ) {
                            this.$message({
                                type: 'info',
                                message: '不能为空'
                            });
                        }
                        else {
                            this.$refs.upload.submit();
                        }
                    },
                    handleAvatarSuccess(res, file) {
                        var zhixiang = this
                        zhixiang.$message({
                            type: 'info',
                            message: '添加成功'
                        });
                    },
                    ware_change(){
                        var zhixiang = this;
                        zhixiang.dialogVisible1 = false;
                        $.get("ware_change", {
                            ID: this.wareID,
                            username:username,
                            biaoti:  zhixiang.ware_c.title,
                            price: zhixiang.ware_c.price,
                            number: zhixiang.ware_c.number,
                        }, function (res) {
                            zhixiang.$message({
                                type: 'success',
                                message: '修改成功!'
                            });
                            zhixiang.tableDataa = JSON.parse(res);
                        })
                    },
                    rowchange(index, row){
                        this.dialogVisible1 = true;
                        this.prcie1 = row.price;
                        this.title = row.biaoti;
                        this.number2 = row.number;
                        this.wareID = row.ID;
                    },
                    handleSelectionChange(val) {
                        this.multipleSelection = val;
                    },
                    buy() {
                        var zhixiang = this;
                        var money = 0;
                        var a;
                        var b;
                        for (var i = 0; i < zhixiang.multipleSelection.length; i++) {
                            var jisuan;
                            a = zhixiang.multipleSelection[i].price
                            b = zhixiang.multipleSelection[i].number1
                            jisuan = a * b;
                            money += jisuan;
                        }
                        zhixiang.money = money;
                        zhixiang.dialogVisible = true;
                        console.log(zhixiang.dialogVisible)
                    },
                    buy_t(){
                        var zhixiang = this;
                        var form = [];
                        zhixiang.dialogVisible = false;
                        for (var i = 0; i < zhixiang.multipleSelection.length; i++) {
                            form = form + ',' + zhixiang.multipleSelection[i].ID
                        }
                        $.get("goumai", {
                            ID: form,
                            username:username,
                            address:  $("#realaddress").val()
                        }, function (res) {
                            zhixiang.$message({
                                type: 'success',
                                message: '购买成功!'
                            });
                            zhixiang.tableData1 = JSON.parse(res);
                        })
                    },
                    rowReturn(index , row){
                        var zhixiang = this;
                        this.$confirm('此操作将回滚该订单, 是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            $.get("rowbank_goods", {
                                username: username,
                                ID: row.ID
                            }, function(res) {
                                zhixiang.$message({
                                    type: 'success',
                                    message: '回滚成功!'
                                });
                                zhixiang.tableData3 = JSON.parse(res);
                            });
                        }).catch(() => {
                            this.$message({
                                type: 'info',
                                message: '已取消回滚'
                            });
                        });
                    },
                    rowReturn1(index , row){
                        var zhixiang = this;
                        this.$confirm('此操作将回滚该订单, 是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            $.get("rowbank_ware", {
                                username: username,
                                ID: row.ID
                            }, function(res) {
                                zhixiang.$message({
                                    type: 'success',
                                    message: '回滚成功!'
                                });
                                zhixiang.tableDatad = JSON.parse(res);
                            });
                        }).catch(() => {
                            this.$message({
                                type: 'info',
                                message: '已取消回滚'
                            });
                        });
                    },
                    rowDelete(index, row) {
                        var zhixiang = this;
                        this.$confirm('此操作将删除该订单, 是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            $.get("Delete_goods", {
                                username: username,
                                ID: row.ID
                            }, function(res) {
                                zhixiang.$message({
                                    type: 'success',
                                    message: '删除成功!'
                                });
                                zhixiang.tableData1 = JSON.parse(res);
                            });
                        }).catch(() => {
                            this.$message({
                                type: 'info',
                                message: '已取消删除'
                            });
                        });
                    },
                    rowDelete1(index, row) {
                        var zhixiang = this;
                        this.$confirm('此操作将删除该商品, 是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            $.get("Delete_ware", {
                                username: username,
                                ID: row.ID
                            }, function(res) {
                                zhixiang.$message({
                                    type: 'success',
                                    message: '删除成功!'
                                });
                                zhixiang.tableDataa = JSON.parse(res);
                            });
                        }).catch(() => {
                            this.$message({
                                type: 'info',
                                message: '已取消删除'
                            });
                        });
                    },
                    formatSum(row){
                        return row.number1 * row.price;
                    },
                    shuliang(row , index){
                        var zhixiang = this;
                       if (parseInt(row.number1) > parseInt(row.number)) {
                           zhixiang.$message({
                               type: 'warning',
                               message: row.biaoti+'商品库存不足!'
                           });
                           return 0;
                       }
                        return 1;
                    },
                    handleClick(key) {
                        var zhixiang = this;
                        if (key.index == 0){
                            $.get("get_dingdan", {
                                username: username
                            }, function(res) {
                                zhixiang.tableData1 = JSON.parse(res);
                            });
                        }
                        else if ( key.index == 1 ){
                            $.get("get_shoumai", {
                                username: username
                            }, function(res) {
                                zhixiang.tableData2 = JSON.parse(res);
                            });
                        }
                        else if ( key.index == 2 ){
                            $.get("get_realding", {
                                username: username
                            }, function(res) {
                                zhixiang.tableData4 = JSON.parse(res);
                            });
                        }
                        else {
                            $.get("get_shanchu", {
                                username: username
                            }, function(res) {
                                zhixiang.tableData3 = JSON.parse(res);
                            });
                        }
                    },
                    handleClick1(key) {
                        var zhixiang = this;
                        if (key.index == 0){
                            $.get("get_ware", {
                                username: username
                            }, function(res) {
                                zhixiang.tableDataa = JSON.parse(res);
                            });
                        }
                        else if ( key.index == 1 ){
                            $.get("get_sware", {
                                username: username
                            }, function(res) {
                                zhixiang.tableDatab = JSON.parse(res);
                            });
                        }
                        else if ( key.index == 2 ){

                        }
                        else {
                            $.get("get_dware", {
                                username: username
                            }, function(res) {
                                zhixiang.tableDatad = JSON.parse(res);
                            });
                        }
                    },
                    tableRowClassName({row, rowIndex}) {
                        if (rowIndex === 1) {
                            return 'warning-row';
                        } else if (rowIndex === 3) {
                            return 'success-row';
                        }
                        return '';
                    },
                    handleSelect(key, keyPath) {
                        var zhixiang = this;
                        if (key != 4){
                            $("#h1,#h2-1,#h2-2,#h2-3,#h3,#h4").css({"opacity":"0","z-index":1});
                            $("#h"+key).css({"opacity":"1","z-index":10});
                        }
                        else {
                            $("#h1,#h2-1,#h2-2,#h2-3,#h3").css({"opacity":"0","z-index":1});
                            $("#h4").css({"opacity":"1","z-index":10});
                            if ( shenfen == 1 ) {
                                $.get("get_dingdan", {
                                    username: username
                                }, function(res) {
                                    zhixiang.tableData1 = JSON.parse(res);
                                });
                                $.get("get_shoumai", {
                                    username: username
                                }, function(res) {
                                    zhixiang.tableData2 = JSON.parse(res);
                                });
                                $.get("get_shanchu", {
                                    username: username
                                }, function(res) {
                                    zhixiang.tableData3 = JSON.parse(res);
                                });
                                $.get("get_realding", {
                                    username: username
                                }, function(res) {
                                    zhixiang.tableData4 = JSON.parse(res);
                                });
                            }
                            else {
                                $.get("get_ware", {
                                    username: username
                                }, function(res) {
                                    zhixiang.tableDataa = JSON.parse(res);
                                });
                                $.get("get_sware", {
                                    username: username
                                }, function(res) {
                                    zhixiang.tableDatab = JSON.parse(res);
                                });
                                $.get("get_dware", {
                                    username: username
                                }, function(res) {
                                    zhixiang.tableDatad = JSON.parse(res);
                                });
                            }
                        }
                    },
                    handleCurrentChange: function (key) {
                        var zhixiang = this;
                        $.get("goods_get", {
                            page: key
                        }, function(resouce) {
                            $("#xianshi").animate({opacity:"0"});
                            setTimeout(function(){ $("#xianshi").animate({opacity:"1"}),zhixiang.res = JSON.parse(resouce) },
                                500);
                        });
                    },
                    handleCurrentChange1: function (key) {
                        var zhixiang = this;
                        $.get("veg_get", {
                            page: key
                        }, function(resouce) {
                            $("#xianshi1").animate({opacity:"0"});
                            setTimeout(function(){ $("#xianshi1").animate({opacity:"1"}),zhixiang.veg = JSON.parse(resouce) },
                                500);
                        });
                    },
                    handleCurrentChange2: function (key) {
                        var zhixiang = this;
                        $.get("fur_get", {
                            page: key
                        }, function(resouce) {
                            $("#xianshi2").animate({opacity:"0"});
                            setTimeout(function(){ $("#xianshi2").animate({opacity:"1"}),zhixiang.fur = JSON.parse(resouce) },
                                500);
                        });
                    },
                    handleCurrentChange3: function (key) {
                        var zhixiang = this;
                        $.get("meal_get", {
                            page: key
                        }, function(resouce) {
                            $("#xianshi3").animate({opacity:"0"});
                            setTimeout(function(){ $("#xianshi3").animate({opacity:"1"}),zhixiang.meal = JSON.parse(resouce) },
                                500);
                        });
                    },
                    dianji(goodsid,prcie,number) {
                        var zhixiang = this;
                        $.get("login_check", {
                        }, function(res) {
                            if (res != 2) {
                                zhixiang.$prompt('该商品' + prcie + '元一斤，请输入需要的斤数', '提示', {
                                    confirmButtonText: '确定',
                                    cancelButtonText: '取消',
                                    inputType: '',
                                    type: 'notice'
                                }).then((value) => {
                                    if ( parseInt(value.value)>parseInt(number)) {
                                        zhixiang.$message({
                                            type: 'info',
                                            message: '添加数量超过库存!'
                                        });
                                    }
                                    else {
                                        $.get("insert_in", {
                                            username: res,
                                            id: goodsid,
                                            number: value
                                        }, function (res) {
                                            if (res == 1) {
                                                zhixiang.$message({
                                                    type: 'success',
                                                    message: '添加购物车成功!'
                                                });
                                            } else {
                                                zhixiang.$message({
                                                    type: 'info',
                                                    message: '添加购物车失败'
                                                });
                                            }
                                        });
                                    }
                                }).catch(() => {
                                    zhixiang.$message({
                                        type: 'info',
                                        message: '取消添加购物车'
                                    });
                                });
                            }
                            else {
                                zhixiang.$confirm('您还没有进行登录, 是否跳转登录?', '提示', {
                                    confirmButtonText: '确定',
                                    cancelButtonText: '取消',
                                    type: 'warning'
                                }).then(() => {
                                    zhixiang.$message({
                                        type: 'success',
                                        message: '跳转成功!'
                                    });
                                    window.location.href = "html/1.html";
                                }).catch(() => {
                                    zhixiang.$message({
                                        type: 'info',
                                        message: '取消跳转'
                                    });
                                });
                            }
                        });

                    },
                }
            }
            var Ctor = Vue.extend(Main)
            new Ctor().$mount('#app')
        });

    });

    var my ={
        methods: {
            open() {
                this.$alert('如果有问题请联系网站维护人员或者加qq咨询', '帮助提示', {
                    confirmButtonText: '确定',
                    callback: action => {

                    }
                });
            }
        }
    }
    var shiyishi = Vue.extend(my)
    new shiyishi().$mount('#tan')

};



